package com.matt.service.impl;

import com.matt.domain.Attachment;
import com.matt.foundation.annotation.OptsTrace;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.query.AttachmentQuery;
import com.matt.repository.AttachmentRepository;
import com.matt.repository.base.AttachmentNativeRepository;
import com.matt.service.AttachmentService;
import com.matt.remoteCall.SsoCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * **********************
 * Created by Ron
 * from com.matt
 * on 2018/6/26 0026.
 * *********************
 */
@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Autowired
    private AttachmentNativeRepository attachmentNativeRepository;

    @Autowired
    private AttachmentRepository attachmentRepository;
    @Autowired
    private SsoCallService ssoCallService;


    @Override
    public Page<Attachment> findPage(AttachmentQuery attachmentQuery, Pageable pageable) {
        List<Attachment> list = attachmentNativeRepository.findPage(attachmentQuery);
        Long count = attachmentNativeRepository.count(attachmentQuery);
        return new PageImpl<Attachment>(list,  PageRequest.of(attachmentQuery.getPageNum(), attachmentQuery.getPageSize()), count);
    }

    @OptsTrace(code = "10005")
    @Override
    public Attachment save(String ticket, Attachment attachment) {
        StaffDTO baseInfo = ssoCallService.getStaffDTO(ticket);
        attachment.setStaffId(baseInfo.getId());
        attachment.setStaffName(baseInfo.getRealName());
        attachment.setCreated(new Date());
        return attachmentRepository.save(attachment);
    }
}
